package com.sharemedia.net
{
	import com.sharemedia.consts.RTMFPServer;
	import com.sharemedia.events.PeerEvent;
	import com.sharemedia.utils.LogUtil;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.GroupSpecifier;
	import flash.net.NetConnection;
	import flash.net.NetGroup;
	import flash.utils.getQualifiedClassName;
	
	import mx.logging.ILogger;
	import mx.logging.Log;

	public class BasePeer extends EventDispatcher
	{
		protected var netConnection:NetConnection;
		
		protected var logger:ILogger;
		
		public var peerId:String;
		
		public function BasePeer()
		{
			logger = LogUtil.getLogger(this);
		}
		
		protected function initConnection():void
		{
			if(netConnection)
				netConnection.removeEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			
			netConnection = new NetConnection();
			netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			netConnection.connect(RTMFPServer.URL);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void
		{
			logger.info(event.info.code);
			
			if(event.info.code == "NetConnection.Connect.Success")
			{
				peerId = netConnection.nearID;
				setupStream();
			}
		}
		
		protected function setupStream():void
		{
			// to override
		}
	}
}